package com.petalsgroup.petals.dao.Comment.Impl;

import com.petalsgroup.petals.dao.Comment.CommentDao;
import com.petalsgroup.petals.pojo.CommentsDO;
import com.petalsgroup.petals.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Timestamp;
import java.util.List;

public class CommentDaoImpl implements CommentDao {
    private final JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<CommentsDO> getAllCommentsList(Integer aid) {
        String sql="select * from comments where aid=? order by like_num desc;";
        try {
            return template.query(sql,new BeanPropertyRowMapper<>(CommentsDO.class),aid);
        }catch (Exception e){}
        return null;
    }

    @Override
    public CommentsDO getMyComment(Integer aid, Integer uid) {
        String sql="select * from comments where aid=? and uid =?;";
        try {
            return template.queryForObject(sql,new BeanPropertyRowMapper<>(CommentsDO.class),aid,uid);
        }catch (Exception e){}
        return null;
    }

    @Override
    public String getAUserName(Integer uid) {
        String sql="select `name` from `user` where uid =?;";
        try {
            return template.queryForObject(sql,String.class,uid);
        }catch (Exception e){}
        return null;
    }

    @Override
    public Integer getAUserComment(int aid, int uid) {
        Integer num=0;
        String sql="select count(*) from comments where aid=? and uid=?;";
        try {
            num=template.queryForObject(sql,Integer.class,aid,uid);
            System.out.println(num);;
        }catch (Exception e){}
        return num;
    }

    @Override
    public void addUserComment(int aid, int uid, String comment_content,Integer score,Timestamp timestamp) {
        String sql="insert into comments (context,create_time,score,aid,uid) values (?,?,?,?,?);";
        try {
            template.update(sql,comment_content,timestamp,score,aid,uid);
        }catch (Exception e){}
    }

    @Override
    public void setUserComment(int aid, int uid, String comment_content,Integer score ,Timestamp timestamp) {
        //System.out.println(aid+","+uid+","+comment_content+","+score+","+timestamp);
        String sql="update comments set context=?,create_time=?,score=? where aid=? and uid=?";
        try {
            template.update(sql,comment_content,timestamp,score,aid,uid);
        }catch (Exception e){}
    }

    @Override
    public void updateAnimationScore(Integer aid) {
        String sql="update animation_simple a set a.score=(select round(avg(c.score),1) from comments c where c.aid=?) where a.aid=?;";
        try {
            template.update(sql,aid,aid);
        }catch (Exception e){}
    }

    @Override
    public void updateAnimationPeopleNum(Integer aid) {
        String sql="update animation_simple set people_num=people_num+1 where aid=?;";
        try {
            template.update(sql,aid);
        }catch (Exception e){}
    }

    @Override
    public int findTotalCount(int aid) {
        String sql="select count(*) from comments where aid=?";
        int total=template.queryForObject(sql,Integer.class,aid);
        return total;
    }

    @Override
    public List<CommentsDO> findByPage(int aid, int start, int num) {
        String sql="select * from(Select * from comments order by like_num desc) comments where aid=? limit ?,?";
        List<CommentsDO> list=null;
        try{
            list=template.query(sql,new BeanPropertyRowMapper<>(CommentsDO.class),aid,start,num);
        }catch (Exception e){}
        return list;
    }
    @Override
    public List<CommentsDO> findByTimePage(int aid, int start, int num) {
        String sql="select * from(select * from comments order by create_time desc) comments where aid=? limit ?,?";
        List<CommentsDO> list=null;
        try{
            list=template.query(sql,new BeanPropertyRowMapper<>(CommentsDO.class),aid,start,num);
        }catch (Exception e){}
        return list;
    }

    @Override
    public int deleteCommentByAid(int aid) {
        String sql = "delete from comments where aid = ?";
        return template.update(sql, aid);
    }

    @Override
    public Integer findUidByComId(int com_id) {
        String sql="select uid from comments where com_id = ?";
        Integer id = template.queryForObject(sql, Integer.class, com_id);
        return id;
    }

    @Override
    public Integer findByUid(int uid, int com_id) {
        String sql="select count(*) from tags where uid=? and com_id=?";
        return template.queryForObject(sql,Integer.class,uid,com_id);
    }

    @Override
    public void removeLikeComment(Integer uid, Integer com_id) {
        String sql="delete from tags where uid=? and com_id=?";
        String updateComment="update comments set like_num=like_num-1 where com_id=?";
        try {
            template.update(sql,uid,com_id);
            template.update(updateComment,com_id);
        }catch (Exception e){}
    }

    @Override
    public void addLikeComment(Integer uid, Integer com_id) {
        String sql="insert into tags(com_id,uid) values(?,?)";
        String updateComment="update comments set like_num=like_num+1 where com_id=?";
        try {
            template.update(sql,com_id,uid);
            template.update(updateComment,com_id);
        }catch (Exception e){}
    }
}
